﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.Persistent;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Core.MyLinq;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Domain.MainBoundedContext.SystemModule
{
    public class DepartmentSpecification : ISpecification<Department, DepartmentSearch>
    {
        #region ISpecification<Department,DepartmentSearch> Members

        public DepartmentSearch SearchParams
        {
            get;
            set;
        }

        #endregion

        #region ISpecification<Department> Members

        public System.Linq.Expressions.Expression<Func<Department, bool>> Expression
        {
            get
            {
                IQueryBuilder<Department> queryBuilder = QueryBuilder.Create<Department>();

                if (null != SearchParams)
                {
                    if (SearchParams.Code.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(ds => ds.Id, SearchParams.Code);
                    }
                    if (SearchParams.Name.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(ds => ds.Name, SearchParams.Name);
                    }
                    if (SearchParams.CorporationCode.IsNotEmpty())
                    {
                         queryBuilder = queryBuilder.Equals(ds => ds.Corporation.Id, SearchParams.CorporationCode);
                    }
                    if (SearchParams.ParentCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(ds => ds.SuperiorDepartment, SearchParams.ParentCode);
                    }
                    queryBuilder = queryBuilder.Equals(ds => ds.IsDeleted, false);
                }

                return queryBuilder.Expression.Expand();
            }
        }

        #endregion
    }
}